//
//  ChpiceCommentViewController.m
//  项目
//
//  Created by 关雷 on 15-1-7.
//  Copyright (c) 2015年 张诗雨. All rights reserved.
//

#import "ChpiceCommentViewController.h"
#import "AFNetWoringYu.h"
#import "ChoiceCommentModel.h"
#import "ChoiceCommentUserModel.h"
#import "UIImageView+WebCache.h"
#import "ChoiceCommentTableViewCell.h"
#import "WebColor.h"
#import "UIButton+WebCache.h"
#import "UIScrollView+MJRefresh.h"
#import "ChoiceDetailViewController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

#define DEFAULTS ([NSUserDefaults standardUserDefaults])
@interface ChpiceCommentViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, assign) NSInteger start;

@end

@implementation ChpiceCommentViewController
- (void)dealloc
{
    [_contentid release];
    [_tableArray release];
    [_userArray release];
    [_table release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.userArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString * body = [NSString stringWithFormat:@"auth=&client=1&contentid=%@&limit=10&start=0", self.contentid];
    [self startConnectWithUrl:@"http://api.pianke.me/comment/getnew" body:body];
    [self createTableView];
    [self footerLoading];
    [self headerRefreshing];
    self.navigationItem.title = @"评论";
//    self.navigationItem.leftBarButtonItem.title = @"11";
    // Do any additional setup after loading the view.
}
- (void)startConnectWithUrl:(NSString *)url body:(NSString *)body
{
    
    [AFNetWoringYu netWorkWithURL:url body:body resultBlock:^(id dic) {
        if ([[DEFAULTS objectForKey:@"Connect"] isEqualToString:@"header"]) {
            [_tableArray removeAllObjects];
        }
        [DEFAULTS setObject:@"normol" forKey:@"Connect"];
        NSDictionary * data = [dic objectForKey:@"data"];
        NSArray * array = [data objectForKey:@"list"];
        for (NSDictionary * adic in array) {
            ChoiceCommentModel * choiceCM = [[ChoiceCommentModel alloc] init];
            [choiceCM setValuesForKeysWithDictionary:adic];
            NSDictionary * userDic = [adic objectForKey:@"userinfo"];
            ChoiceCommentUserModel * userModel = [[ChoiceCommentUserModel alloc] init];
            [userModel setValuesForKeysWithDictionary:userDic];
            [_tableArray addObject:choiceCM];
            [_userArray addObject:userModel];
            [choiceCM release];
            [userModel release];
        }
        [self.table headerEndRefreshing];
        [_table reloadData];
        
    }];
}
- (void)createTableView
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 45)];
    _table.dataSource = self;
    _table.delegate = self;
    [_table registerClass:[ChoiceCommentTableViewCell class] forCellReuseIdentifier:@"choiceCommentCell"];
    [self.view addSubview:_table];
    [_table release];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setSeparatorColor:[UIColor clearColor]];
    
    ChoiceCommentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"choiceCommentCell"];
   ChoiceCommentModel * choiceCM =  [_tableArray objectAtIndex:indexPath.row];
    ChoiceCommentUserModel * userModel = [_userArray objectAtIndex:indexPath.row];
    
    [cell.imageBu setBackgroundImageWithURL:[NSURL URLWithString:userModel.icon] forState:UIControlStateNormal];
    [cell.nameBu setTitleColor:[WebColor blue] forState:UIControlStateNormal];
    [cell.nameBu setTitle:userModel.uname forState:UIControlStateNormal];
    cell.timeLable.text = choiceCM.addtime_f;
    cell.contentLable.text = choiceCM.content;
    cell.layer.borderWidth = 1.0;
    cell.layer.borderColor = [[UIColor grayColor] CGColor];
    cell.backgroundColor = [WebColor whiteSmoke];
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
//上拉加载
- (void)footerLoading
{
    self.start = 7;
    [self.table addFooterWithCallback:^{
        if (_start < self.number) {
            NSString * body = [NSString stringWithFormat:@"auth=&client=1&contentid=%@&limit=10&start=%ld", self.contentid, _start];
            [self startConnectWithUrl:@"http://api.pianke.me/comment/getnew" body:body];
            [self.table footerEndRefreshing];
            _start += 7;
        }
        
    }];
}
//下拉刷新
- (void)headerRefreshing
{
    
    [self.table addHeaderWithCallback:^{
        [DEFAULTS setObject:@"header" forKey:@"Connect"];
         NSString * body = [NSString stringWithFormat:@"auth=&client=1&contentid=%@&limit=10&start=0", self.contentid];
         [self startConnectWithUrl:@"http://api.pianke.me/comment/getnew" body:body];
        [_table reloadData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
